﻿/*
==============================================================================
// 出库单管理 页面控制层代码。
==============================================================================
*/
(function ($) {

    if (!window.platform) { window.platform = new Object(); }
    if (!window.platform.RequisitionBillManage) { window.platform.RequisitionBillManage = new Object(); }

    window.platform.RequisitionBillManage.initPage = function (createFunction, editFunction, removeFunction) {

        var _bindQueryButtonEvent = function () {
            $("#aquery", "#queryFm").bind("click", function () {
                var fm = $('#queryFm').serialize();
                $('#dg').datagrid('load', { model: fm });
            });
        };

        var _bindToolBarButtonEvent = function () {
            $("#acreate", "#toolbar").bind("click", function () {
                _createRequisitionBill();
            });
            $("#aedit", "#toolbar").bind("click", function () {
                _editRequisitionBill();
            });
            $("#aremove", "#toolbar").bind("click", function () {
                _singleAction();
            });

            $("#areload", "#toolbar").bind("click", function () {
                _reloadRequisitionBill();
            });
            $("#ashowdetail", "#toolbar").bind("click", function () {
                _showRequisitionBillDetailToolbar();
            });
            $("#aprintview", "#toolbar").bind("click", function () {
                _printRequisitionBillToolbar();
            });
            $("#asave", "#tb").bind("click", function () {

                _saveRequisitionBill($('#dd'));

            });
            $("#aclose", "#tb").bind("click", function () {

                $('#dd').dialog('close');


            });
        };

        var _requisitionBillDataGridInit = function () {
            var options = {
                singleSelect: true,
                checkOnSelect: false,
                selectOnCheck: false,
                fit: true,
                border: false,
                rownumbers: true,
                nowrap: true,
                striped: true,
                remoteSort: false,
                url: "/RequisitionManage/RequisitionBillJson",
                queryParams: { model: $('#queryFm').serialize() },
                idField: "ID",
                frozenColumns: [[

                      { field: 'Name', title: '出库单名称', width: 150, align: 'center', sortable: true },

                ]],
                columns: [[
                { field: 'Code', title: '编号', width: 100, align: 'center', sortable: true },
                { field: 'UseDepartment', title: '领用部门', width: 100, align: 'center', sortable: true },
                { field: 'CustomerName', title: '客户姓名，组别', width: 150, align: 'center', sortable: true },
                 { field: 'SourceProperties', title: '货源性质', width: 170, align: 'center', sortable: true },
                    { field: 'UsePerson', title: '领用人', width: 60, align: 'center', sortable: true },
                       { field: 'Bookkeeper', title: '记账人', width: 60, align: 'center', sortable: true },
                        { field: 'DeliverGoodsPerson', title: '发货人', width: 60, align: 'center', sortable: true },
                   { field: 'OutBoundTime', title: '出库时间', width: 100, align: 'center', sortable: true },
                   { field: 'SortNumber', title: '排序号', width: 50, align: 'center', sortable: true },

                   {
                       field: 'Action', title: '操作', width: 140, align: 'center',
                       formatter: function (value, row, index) {
                           var e = "";
                           e += '<a href="javascript:void(0);" style="text-decoration:none" onclick="_showRequisitionBillDetail(' + row.ID + ')">出库单明细</a>&nbsp;&nbsp;';
                           e += '<a href="javascript:void(0);"  style="text-decoration:none" onclick="_printRequisitionBill(' + row.ID + ')">出库单打印</a>&nbsp;&nbsp;';
                           return e;
                          

                       }


                   },

                ]],
                pagination: true,

                toolbar: "#toolbar",
                onDblClickRow: function (rowIndex, rowData) {
                    if (editFunction) {
                        _editRequisitionBill(rowData);
                    }
                }

            };

            $("#dg").datagrid(options);
        };



        _bindQueryButtonEvent();
        _bindToolBarButtonEvent();
        _requisitionBillDataGridInit();
    };



    _createRequisitionBill = function () {
        $('#dd').dialog({
            title: '新建出库单',
            width: 750,
            height: 400,
            closed: false,
            cache: false,
            resizable: true,
            href: '/RequisitionManage/CreateEdit?id=0',
            modal: true,
            buttons: '#tb'


        });

    };
    _showRequisitionBillDetail = function (id) {

        OpenNewTab("出库单明细", "/RequisitionDetailManage/Index?requisitionBillID=" + id);
    }
    _showRequisitionBillDetailToolbar = function (row) {
        row = row ? row : $("#dg").datagrid("getSelected");
        if (row) {
            OpenNewTab("出库单明细", "/RequisitionDetailManage/Index?requisitionBillID=" + row.ID);
        }
        else {
            $.messager.alert("警告提醒", "请选择一行");
        }
    }
    _printRequisitionBill = function (id) {
        window.open("/RequisitionManage/PrintRequisitionBill?id=" + id);
    }
    _printRequisitionBillToolbar = function (row) {
        row = row ? row : $("#dg").datagrid("getSelected");
        if (row) {
            window.open("/RequisitionManage/PrintRequisitionBill?id=" + row.ID);
        }
        else {
            $.messager.alert("警告提醒", "请选择一行");
        }

    }
    _editRequisitionBill = function (row) {
        row = row ? row : $("#dg").datagrid("getSelected");

        if (row) {
            $('#dd').dialog({
                title: '编辑出库单',
                width: 750,
                height: 400,
                closed: false,
                cache: false,
                resizable: true,
                href: '/RequisitionManage/CreateEdit?id=' + row.ID,
                modal: true,
                buttons: '#tb'


            });
        }
        else {
            $.messager.alert("警告提醒", "请选择一行");
        }
    };

    _saveRequisitionBill = function (dialog) {
        var verifyResult = $("form", dialog).form("validate");
        if (!verifyResult) { return false };

        var requisitionBill = $(dialog).form().serializeObject();
        var _callback = function (result) {
            if (result.success) {
                //$.messager.alert('操作提醒', '操作成功');
                _reloadRequisitionBill();
            } else {
                $.messager.alert('错误提醒', result.msg, 'error');
            }
            $('#dd').dialog('close');
        };

        $.post("/RequisitionManage/Save", requisitionBill, function (result) {
            _callback.call(this, result);
        }, 'json');
    };

    _reloadRequisitionBill = function () {
        $('#dg').datagrid('clearChecked');
        $('#dg').datagrid('clearSelections');
        $('#dg').datagrid('reload');
    };

    _batchAction = function () {
        var rows = $('#dg').datagrid('getSelected');
        var data = [];
        if (rows.length != 0) {
            for (var i = 0; i < rows.length; i++) {
                data.push(rows[i].ID);
            }
        }

        else {
            $.messager.alert("请选择一行");
        }

        if (data.length != 0) {
            $.messager.confirm('确认', "您确定要删除所选入库单吗", function (r) {
                if (r) {
                    $.post("/RequisitionManage/BatchDelete", { ids: data.join(',') }, function (result) {
                        if (result.success) {
                            //$.messager.alert('操作提醒', '操作成功');
                            _reloadRequisitionBill();
                        } else {
                            $.messager.alert('错误提醒', result.msg, 'error');
                        }
                    }, "json");
                }
            });
        }

    };

    _singleAction = function (row) {
        row = row == undefined ? $('#dg').datagrid("getSelected") : row;
        if (row) {
            $.messager.confirm('确认', "您确定要删除该入库单吗", function (r) {
                if (r) {
                    $.post("/RequisitionManage/SingleDelete", { id: row.ID }, function (result) {
                        if (result.success) {
                            //$.messager.alert('操作提醒', '操作成功');
                            _reloadRequisitionBill();
                        } else {
                            $.messager.alert('错误提醒', result.msg, 'error');
                        }
                    }, "json");
                }
            });
        }
        else {
            $.messager.alert("警告提醒", "请选择一行");
        }
    }


})(jQuery);